﻿using SmartPlant.Model;
using SmartPlant.Model.DbContext;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace SmartPlant.Services
{
    public class SysServiceInit : BaseService, ISysServiceInit
    {
        private IDynamicDataApi _dynamicDataApi;
        private IItemListService _listService;
        public SysServiceInit()
        {
            _dynamicDataApi = new DynamicDataApiService();
            _listService = new ItemListService();
        }

        /// <summary>
        /// 相当于Server端domain-conn那个函数
        /// </summary>
        /// <param name="proId">此时<see cref="GlobalObject.CurrentProject"/>还么有赋值 </param>
        /// <returns></returns>
        public async Task InitAsync(string proId)
        {
            var task1 = Task.Run(async () => await this.GetCodelistsAsync(proId));
            var task2 = Task.Run(async () => await this.GetLookupsAsync(proId));
            var task3 = Task.Run(async () => await this.GetUoM(proId));
            var task4 = Task.Run(async () => await this.GetItemAttrs(proId, "instrument"));


            var list = await _dynamicDataApi.InitIndexTablePropertyInfosAsync(proId);
            var task6 = Task.Run(async () => await _dynamicDataApi.InitIndexTablePropertyDropDownListAsync(proId, list));







            //后面就比较快
            var X = Stopwatch.StartNew();
            var task7 = Task.Run(async () => await _dynamicDataApi.InitDrawingDocPropertyInfosAsync(proId));
            X.Stop();
            Console.WriteLine("InitDrawingDocPropertyInfosAsync: " + X.ElapsedMilliseconds);
            X.Restart();
            var task8 = Task.Run(async () => await _listService.InitPropertyInfosAsync(proId, GroupMode.mode0));
            X.Stop();
            Console.WriteLine("InitPropertyInfosAsync1: " + X.ElapsedMilliseconds);
            X.Restart();
            var task9 = Task.Run(async () => await _listService.InitPropertyInfosAsync(proId, GroupMode.mode1));
            X.Stop();
            Console.WriteLine("InitPropertyInfosAsync2: " + X.ElapsedMilliseconds);
            X.Restart();
            var task10 = Task.Run(async () => await _listService.InitPropertyInfosAsync(proId, GroupMode.mode2));
            X.Stop();
            Console.WriteLine("InitPropertyInfosAsync3: " + X.ElapsedMilliseconds);
            X.Restart();
            var task11 = Task.Run(async () => await _listService.InitHookUpItemPropertyInfosAsync(proId));
            X.Stop();
            Console.WriteLine("InitHookUpItemPropertyInfosAsync: " + X.ElapsedMilliseconds);
            X.Restart();
            var task12 = Task.Run(async () => await _listService.InitHookUpComponentPropertyInfosAsync(proId));
            X.Stop();
            Console.WriteLine("InitHookUpComponentPropertyInfosAsync: " + X.ElapsedMilliseconds);


            await Task.WhenAll(new Task[] { task1, task2, task3, task4, task6, task7, task8, task9, task10, task11, task12 });
        }


        #region 1
        public async Task GetUoM(string ProjId)
        {
            var uri = $"/api/project/{ProjId}/codelists-api/all-Uo-Ms";
            var res2 = await GetAsync<List<UnitOfMeasure>>(uri);
            GlobalObject.UnitOfMeasureCollection.Clear();
            GlobalObject.UnitOfMeasureCollection = res2.data;
        }


        public async Task GetLookupsAsync(string ProjId)
        {
            string uri = $"/api/Project/{ProjId}/codelists-api/all-lookups";
            var res = await GetAsync<List<Lookuptables>>(uri);


            GlobalObject.LookuptablesCollection.Clear();
            GlobalObject.LookuptablesCollection = res.data;


        }
        public async Task GetCodelistsAsync(string ProjId)
        {
            string uri = $"/api/Project/{ProjId}/codelists-api/all-codelists";
            var res = await GetAsync<List<Enumeration>>(uri);

            GlobalObject.EnumerationsCollection.Clear();
            GlobalObject.EnumerationsCollection = res.data;

        }


        /// <summary>
        /// 尽量别用，很慢，数据太大
        /// </summary>
        /// <param name="ProjId"></param>
        /// <param name="Ids">逗号</param>
        /// <returns></returns>
        public async Task GetItemAttrs(string ProjId, string ItemId)
        {
            var uri = $"/api/project/{ProjId}/itemattributions-api/item-attributions-list/{ItemId}";
            var res2 = await GetAsync<List<Itemattributions>>(uri);
            foreach (var item in res2.data)
            {
                if (!GlobalObject.ItemattributionsCollection.Any(x => x.Id == item.Id))
                {
                    GlobalObject.ItemattributionsCollection.Add(item);
                }

            }

        }
        /// <summary>
        /// 查询某一个。填充到<see cref="GlobalObject.ItemattributionsCollection"/>。如果没有，就从服务器查询一下
        /// </summary>
        /// <param name="ProjId"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Itemattributions> GetItemAttr(string ProjId, decimal Id)
        {
            var EXIST = GlobalObject.ItemattributionsCollection.FirstOrDefault(x => x.Id == Id);
            if (EXIST == null)
            {
                var uri = $"/api/project/{ProjId}/itemattributions-api/Single/{Id}";
                var res2 = await GetAsync<Itemattributions>(uri);
                GlobalObject.ItemattributionsCollection.Add(res2.data);
                return res2.data;
            }
            else
            {
                return EXIST;
            }

        }
        #endregion
    }
}
